/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Specialized;
using System.ComponentModel.Design;
using System.IO;
using System.Text;

using EnvDTE;

using Microsoft.Practices.Common;
using Microsoft.Practices.Common.Services;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using Microsoft.Practices.RecipeFramework.VisualStudio;
using Microsoft.Practices.RecipeFramework.Services;

namespace DslEditorPowerToy.Recipes.ValueProviders
{
    public abstract class ValueProviderBase : ValueProvider, IAttributesConfigurable
    {
        private EnvDTE.DTE _dte = null;
        private ExpressionEvaluationService _evaluator = null;

        #region Constructors
        protected ValueProviderBase()
            : base()
        {
            this._evaluator = new ExpressionEvaluationService();
        }
        #endregion

        #region IAttributesConfigurable Members
        public virtual void Configure(StringDictionary attributes)
        {
        }

        #endregion

        #region Protected Properties
        [CLSCompliant(false)]
        protected EnvDTE.DTE DTE
        {
            get
            {
                if (null == this._dte)
                    this._dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE));

                return this._dte;
            }
        }
        /// <summary>
        /// Gets path to this guidance package
        /// </summary>
        protected string PackagePath
        {
            get
            {
                TypeResolutionService resolution = (TypeResolutionService)this.GetService<ITypeResolutionService>();
                return resolution.BasePath;
            }
        }
        /// <summary>
        /// Returns the path to this guidance package 'templates' directory
        /// </summary>
        protected string PackageTemplatePath
        {
            get
            {
                return Path.Combine(this.PackagePath, "Templates");
            }
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Evaluates the specified parameter using dictionary of all arguments of the template
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        protected object Evaluate(string parameter)
        {
            //Get all arguments
            IDictionaryService dictionaryService = (IDictionaryService)this.GetService(typeof(IDictionaryService));

            //Evaluate
            return this._evaluator.Evaluate(parameter, new ServiceAdapterDictionary(dictionaryService));
        }
        /// <summary>
        /// Returns the value of the specified argument
        /// </summary>
        /// <param name="_newValue"></param>
        /// <returns></returns>
        protected object GetArgumentValue(string argumentName)
        {
            //Get all arguments in template
            IDictionaryService dictionaryService = (IDictionaryService)this.GetService(typeof(IDictionaryService));

            return dictionaryService.GetValue(argumentName);
        }
        #endregion
    }
}
